<?php

namespace app\pc\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class OrderDetails extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("OrderDetails");
        parent::__construct();
        $rows = Cache::get($this->Cstatic->get("sid"));
        $this->sid_user_id = $rows['id'];
        $this->sid_mobile = $rows['mobile'];

    }

    /**
 * @return \think\response\Json  委托列表
 */
    public function index()
    {//
        //搜索条件 时间 用户号码 状态
        if(isset($this->status)){
            $where['a.status']=$this->status;
        }else{
        // $where['a.status']=['in',"0,1"];
         $where['a.status']=0;
            // $where['a.status']=['OR',[0,1]];

        }
        $where['user_id'] =$this->sid_user_id;

        //时间的处理
        $res = $this->model->OrderDetails_page($where);


        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }

    /**
     * @return \think\response\Json  委托列表
     */
    public function all()
    {//
        //搜索条件 时间 用户号码 状态

        $where['user_id'] =$this->sid_user_id;
        //时间的处理

        $res = $this->model->OrderDetails_page($where);
     /*   status 0 1
    is_sell_status   1 2*/

        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }

    public function wt(){
        $where['user_id'] =$this->sid_user_id;
        //时间的处理
        $res = $this->model->OrderDetails_page2($where);

        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }

    public function  position()
    {//
        //搜索条件 时间 用户号码 状态
        $where['a.status']=2;
        $where['a.user_id']=$this->sid_user_id;
        //时间的处理
        $res = $this->model->OrderDetails_page($where);
        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }



    //撤单  订单改为废单
    public function revokeapply(){

        $order = $this->model->field("*,created_at created_ats")->where(['id'=>$this->id])->find();
        if($order['status']==4){
            return   failJson("该订单已撤单");//审核中
        }
        if($order['status']==2){
            $status =  $this->model->save(['is_sell_status'=>0],['id'=>$this->id,"user_id"=>$this->sid_user_id]);
            if($status){
                return   sucessJson("撤单成功");//审核中
            }
            return   failJson("撤单失败");//审核中

        }

        if($order['status']==3){
            return   failJson("该订单已卖出");//审核中
        }
        if(Cache::has("SiteCofig")){
          $siteConfig =   Cache::get("SiteCofig");
        }else{
         $siteMode = new \app\admin\model\Site();
          $siteConfig = $siteMode->where("id",1)->find();
        }
        $usedtime = bcdiv((time()-$order['created_ats']),60);
        if(empty($siteConfig['time_out_fail'])){
            $siteConfig['time_out_fail'] = 5;
        }
        if($usedtime>$siteConfig['time_out_fail']){
            return   failJson("{$siteConfig['time_out_fail']}分钟内可撤单,撤单时间已失效");//审核中
        }
        $status =  $this->model->revokeapply($this->id,$this->sid_user_id);//撤单申请

       // $status =  $this->model->save(['status'=>4],['id'=>$this->id,"user_id"=>$this->sid_user_id]);
        if($status['status']){
            //冻结金额返回
            return   sucessJson($status['msg']);//审核中
        }else{
            return   failJson($status['msg']);//审核中
        }

    }


    public function sellapply(){
        //前台提交持仓到卖出的申请
        $order = $this->model->where(['id'=>$this->id])->find();
        if($order['status']!=2){
            return   failJson($this->ecLang("failsstatus"));//审核中
            //0未审核 1审核中  2持仓中 3卖出 4废单
        }
        $status =  $this->model->save(['is_sell_status'=>1],['id'=>$this->id,"user_id"=>$this->sid_user_id]);
        if($status){
            return   sucessJson($this->ecLang("sucessstatus"));//审核中
        }else{
            return   failJson($this->ecLang("failtatus"));//审核中
        }
    }


    public function sellapplymany(){
        $ids_ = explode(",",$this->ids);
        $where = [];
        if(!empty($ids_)){
            $ids_ = array_filter($ids_,function($val){
                return is_numeric($val);
            });
            $len = count(array_filter($ids_));
            if(!empty($ids_))
            {
                $where['id'] = ["in",$ids_];
                $where['status'] = 2;

                //其他查詢條件
            }
            //
            $count = $this->model->where($where)->count();

            if($len!=$count){
                return   failJson($this->ecLang("parmasfail"));

            }
            $where['user_id'] = $this->sid_user_id;

            $status =  $this->model->update(['is_sell_status'=>1],$where);
            if($status){
                return   sucessJson($this->ecLang("sucessstatus"));//审核中
            }else{
                return   failJson($this->ecLang("failtatus"));//审核中
            }

        }
    }

    public function read($id){

        $res =  $this->model->field("*,mouth_config_id as dr,CONCAT(TRUNCATE(manage_per/1,2),'%') as manage_per")->where("id",$id)->find();
        if(!$res){

            return   failJson($this->ecLang("failInsert"));
        }
        $res['mobile'] = $this->sid_mobile;
        $Shares = model("Shares");
       $d = $Shares->where("id",$res['stock_id'])->find();
        $res['name'] = $d['name'];
        $res['code'] = $d['code'];
        $res['attribute'] = $d['attribute'];
        $res['lastprice'] = gethq($res['code']);

        return sucessJson($this->ecLang("scuessInsert"),$res->toArray());
    }






}
